define([
	'jquery',
	'layer',
	'idangerous',
	'selectModify',
	'../../Shop/module/STH_main.js',
], function($, layer, swiper, selectModify, main) {

	return {
		swiperAddM: function(con) {
			var _this = this,
				key = con.key,
				order = con.order,
				autoplay = con.prop.autoplay,
				prop = con.prop,
				ItemList = con.ItemList,
				listLng = ItemList.length,
				newPanel = main.getPanel("swiper"),
				newEdit = main.getEdit("swiper");

			//属性操作
			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newEdit).attr("key", key);

			$(newEdit).find("input[name='autoplay']").val(autoplay);
			$(newPanel).find(".swiper-wrapper").html("");
			$(newEdit).find(".sw_choices").html("");
			var newPanelLi = $(newPanel).find("#imagesmb").html(),
				newEditLi = $(newEdit).find("#imagesmb").html();

			for(var d = 0; d < listLng; d++) {
				var row = ItemList[d];
				var newob = {
					"tIndex": d,
					"url": "#",
					"urldata": row.Fsrc,
					"Fname": row.Ftypename,
					"title": row.Ftitle
				};

				var nPLi = main.formatTemplate(newob, newPanelLi),
					nELi = main.formatTemplate(newob, newEditLi);
				$(newPanel).find(".swiper-wrapper").append(nPLi);
				$(newEdit).find(".sw_choices").append(nELi);
 

				$(newEdit).find(".sw_choices .com-list-box-choice").eq(d).find("select.sw_sel1").val(row.Ftype);
				//$(newEdit).find(".sw_choices .com-list-box-choice").eq(d).find(".sw_select2 .weixin_url").val(newob.Fname);

			}

			$(newEdit).css("display", "none");
			$(".edit-panel").append(newEdit);

			//延时顺序重定位
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}

			_this.swiperActive(newPanel, newEdit, key);

		},

		swiperActive: function(thisPanel, thisEdit, key) {
			$(thisPanel).find('.swiper-container').attr("id", "swp" + key);
			var _this = this,
				upload_index = 0,
				mySwiper, imgloadinit, imgId = "imgupload" + key,
				swiper_config = {
					speed: 1000,
					pagination: "#swp" + key + " .swiper-pagination",
					slidesPerView: 1,
					autoplay: 0,
					loop: true,
					autoplayDisableOnInteraction: false,
					onSlideChangeEnd: function(s) {
						//console.log(s.params);
					}
				};
			if(typeof(g_data[key].prop.autoplay) == "undefined") {
				g_data[key].prop = {
					"autoplay": 0,
				};
				g_data[key].ItemList[0] = {
					"Fsrc": "",
					"Ftypeid": "",
					"Ftitle": "",
					"Ftype": "",
					"Fname": "请选择"
				};
			}

			//图片上传初始化
			$(thisEdit).find(".imgupload").attr("id", imgId);

			//初始化swiper
			$(thisPanel).find('.swiper-container').attr("id", "swp" + key);

			function creatSwiper() {
				var autoplay = g_data[key].prop.autoplay * 1000;
				swiper_config.autoplay = autoplay;
				mySwiper = new Swiper('#swp' + key, swiper_config);
			}

			//参数调整函数
			function propChange(thisPanel, thisEdit, key) {
				$(thisEdit).find("input[name='autoplay']").unbind('blur').blur(function() {
					var autoplayVal = filterXSS(main.html_encode($(this).val()));
					if(!(/^[0-9]{1}$/.test(autoplayVal) || /^10$/.test(autoplayVal))) {
						layer.msg("输入的秒数不适合", {
							icon: 0,
							time: 1500
						});
						$(this).val(0);
						autoplayVal = 0;
					} else {
						autoplayVal = parseInt(autoplayVal);
					}
					mySwiper.params.autoplay = autoplayVal * 1000;
					g_data[key].prop.autoplay = autoplayVal;
					if(autoplayVal > 0) mySwiper.startAutoplay();
				});
			}

			//添加图片
			function addImg(thisPanel, thisEdit, key) {
				$(thisEdit).find(".sw_imgan").unbind('click').click(function() {
					var thisPanelLiy = $(thisPanel).find("#imagesmb").html(),
						thisEditLiy = $(thisEdit).find("#imagesmb").html(),
						index1 = $(thisEdit).find('.sw_choices>li').length,
						thisye = {};
					thisye1 = {};
					thisye.imgid = "";
					thisye.urldata = "../../css/skin/default/add_pic.png";
					thisye.title = "";
					thisye.url = "";
					thisye1.tIndex = index1;
					thisye.Fname = "";

					var tPLiy = main.formatTemplate(thisye1, thisPanelLiy),
						tELiy = main.formatTemplate(thisye, thisEditLiy);
					if($(thisEdit).find('.sw_choices>li').length < 8) {
						mySwiper.appendSlide($(tPLiy).html());
						$(thisPanel).find(".swiper-slide a[tIndex='" + index1 + "']").parent().attr("tIndex", index1);
						$(thisEdit).find(".sw_choices").append(tELiy);
						g_data[key].ItemList.push({
							"Fsrc": "",
							"Ftitle": "",
							"Ftype": "",
							"Ftypeid": "0",
							"Fname": "请选择"
						});
					} else {
						layer.msg('图片项不得超过8个', {
							"icon": 0
						});
					}
					selectOne();
					selectTwo();
					initText(thisPanel, thisEdit, key);
					imgUpload(thisPanel, thisEdit, key);
					imgDelete(thisPanel, thisEdit, key);
					infoEdit(thisPanel, thisEdit, key);
				});
			}

			function initText(thisPanel, thisEdit, key) {
				$(thisEdit).find(".sw_choices .weipage_url").unbind("click").click(function() {
					var index = $(thisEdit).find(".sw_choices .weipage_url").index(this);

					var selText = $(this).parents(".controls").find("select.sw_sel1").val();
					var weipage_url = $(this);

					g_data[key].ItemList[index].Ftype = selText;
					if(selText == "1") { //微页面
						main.commonDialog('vstore/tb_page/Sql.html', '选择微页面', 800, 600, function(res) {

							$(weipage_url).val(res.PageName);
							g_data[key].ItemList[index].Ftypeid = res.ID;

						});

					} else if(selText == "2") { //商品分类
						main.typeDialog(function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "3") { //单商品
						main.productDialog('', function(res) {
							$(weipage_url).val(res[0].Fsummary);
							g_data[key].ItemList[index].Ftypeid = res[0].Fid;
						});

					} else if(selText == "4") { //闪购活动
						main.commonDialog('vstore/tb_activity_time/Sql.html', '闪购活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "5") { //团购活动
						main.commonDialog('vstore/tb_activity_group/Sql.html', '团购活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "6") { //拼团活动
						main.commonDialog('vstore/tb_activity_group_user/Sql.html', '拼团活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					}
				});

			}
			//图片删除 移动功能 
			function imgDelete(thisPanel, thisEdit, key) {

				//删除
				$(thisEdit).undelegate(".com-list-box-delete i", 'click').delegate(".com-list-box-delete i", 'click', function() {
					var shunxu = $(thisEdit).find(".com-list-box-delete i").index(this),
						thisEditLiy = $(thisEdit).find("#imagesmb").html(),
						nowLen1 = $(thisEdit).find(".sw_choices>li").length,
						thisye = {};
					thisye.imgid = "";
					thisye.urldata = "";
					thisye.title = "";
					thisye.url = "#";
					var tELiy = main.formatTemplate(thisye, thisEditLiy);

					if(nowLen1 <= 1) {
						$(thisEdit).find(".sw_choices").html("");
						$(thisEdit).find(".sw_choices").append(tELiy);
						mySwiper.removeAllSlides();
						var newli = "<a href='#' tIndex='0'><img src='" + app.baseUrl + "/seller/images/shop_decorate/swiper-img.jpg'></a>";
						mySwiper.appendSlide(newli);
						$(thisPanel).find(".swiper-slide a[tIndex='0']").parent().attr("tIndex", "0");
						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						infoEdit(thisPanel, thisEdit, key);
					} else {
						$(this).parent().parent().remove();
						mySwiper.removeSlide(shunxu);
						$(thisPanel).find(".swiper-slide").each(function() {
							var tIndex = $(this).attr("tIndex");
							if(tIndex > shunxu) {
								$(this).attr("tIndex", (tIndex - 1));
							}
						});
					}
					g_data[key].ItemList.splice(shunxu, 1);
				});

				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {
					var index = $(thisEdit).find(".com-list-box-operate .arrow-up").index(this);
					if(index > 0) {
						var htmlE = $(this).parents("li").prop("outerHTML"),
							htmlP0 = $(thisPanel).find(".swiper-slide[tIndex='" + index + "']").html(),
							htmlP1 = $(thisPanel).find(".swiper-slide[tIndex='" + (index - 1) + "']").html(),
							sel1val = $(thisEdit).find(".sw_choices>li:eq(" + (index) + ")").find("select.sw_sel1").val();
						$(thisEdit).find(".sw_choices>li:eq(" + (index - 1) + ")").before(htmlE);
						$(thisEdit).find(".sw_choices>li:eq(" + (index + 1) + ")").remove();
						$(thisPanel).find(".swiper-slide[tIndex='" + index + "']").html(htmlP1);
						$(thisPanel).find(".swiper-slide[tIndex='" + (index - 1) + "']").html(htmlP0);
						$(thisEdit).find(".sw_choices>li:eq(" + (index - 1) + ")").find("select.sw_sel1").val(sel1val);
						main.exchange(g_data[key].ItemList, (index - 1), index);
						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						infoEdit(thisPanel, thisEdit, key)
					}

				});

				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					var index = $(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box-operate .arrow-down").length;

					if(index < (lilen - 1)) {
						var htmlE = $(this).parents("li").prop("outerHTML"),
							htmlP0 = $(thisPanel).find(".swiper-slide[tIndex='" + index + "']").html(),
							htmlP1 = $(thisPanel).find(".swiper-slide[tIndex='" + (index + 1) + "']").html(),
							sel1val = $(thisEdit).find(".sw_choices>li:eq(" + (index) + ")").find("select.sw_sel1").val();
						$(thisEdit).find(".sw_choices>li:eq(" + (index + 1) + ")").after(htmlE);
						$(thisEdit).find(".sw_choices>li:eq(" + index + ")").remove();
						$(thisPanel).find(".swiper-slide[tIndex='" + index + "']").html(htmlP1);
						$(thisPanel).find(".swiper-slide[tIndex='" + (index + 1) + "']").html(htmlP0);
						main.exchange(g_data[key].ItemList, index, (index + 1));
						$(thisEdit).find(".sw_choices>li:eq(" + (index + 1) + ")").find("select.sw_sel1").val(sel1val);
						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						infoEdit(thisPanel, thisEdit, key);
					}
				});

			}

			//图片上传操作
			function imgUpload(thisPanel, thisEdit, key) {
				$(thisEdit).find(".com-list-box-choice .com-list-box-choice-pic").unbind("click").click(function() {
					upload_index = $(thisEdit).find(".com-list-box-choice .com-list-box-choice-pic").index(this);

					main.photoDialog(function(res) {
						var path = res[0].replace(small, middle);

						$(thisPanel).find(".STH_title_logo").attr("src", path);
						$(thisEdit).find(".STH_title_img img").attr("src", path);
						$(thisEdit).find(".com-list-box-choice").eq(upload_index).find(".com-list-box-choice-pic img").attr("src", path);
						$(thisPanel).find(".swiper-slide[tIndex='" + upload_index + "'] img").attr("src", path);
						//改变数据 
						g_data[key].ItemList[upload_index].Fsrc = path;

					});
				});
			}

			//一级select
			function selectOne() {

				$(thisEdit).find('.sw_choices .sw_sel1').selectModify({
					onSelectChange: function(e, attrs) {
						var one_val = $(e).val(),
							onindex = $(e).parents(".com-list-box .com-list-box-choice").index();

						g_data[key].prop.Ftype = one_val;

						selectTwo(onindex);
					}
				});

			}

			//二级select函数
			function selectTwo(t) {
				$(thisEdit).find('.sw_choices .weipage_url').eq(t).val("");
				var item = g_data[key].ItemList[t];
				if(item) {
					item.Ftypeid = 0;
					item.Ftype = 0;
				}

			}

			//信息编辑
			function infoEdit(thisPanel, thisEdit, key) {
				$(thisEdit).find("input[name='title']").unbind('blur').blur(function() {
					var titleVal = filterXSS(main.html_encode($(this).val())),
						shunxu = $(thisEdit).find("input[name='title']").index(this);
					if(titleVal.length > 30) {
						titleVal = titleVal.slice(0, 30);
					}

					$(this).val(titleVal);
					if(g_data[key].ItemList[shunxu]) {
						g_data[key].ItemList[shunxu].Ftitle = titleVal;
					} else {
						g_data[key].ItemList[shunxu] = {
							"Ftitle": titleVal
						};
					}

				});
			}

			creatSwiper();

			addImg(thisPanel, thisEdit, key);
			selectOne();
			selectTwo();
			imgUpload(thisPanel, thisEdit, key);
			propChange(thisPanel, thisEdit, key);
			imgDelete(thisPanel, thisEdit, key);
			infoEdit(thisPanel, thisEdit, key);
			initText(thisPanel, thisEdit, key);

			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}
	}
});